﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO;
using System.Collections;
using CCWin;

namespace SCCM
{
    public partial class HistoryWindow : Skin_Mac
    {
        private DateTime dt1=new DateTime(), dt2=new DateTime();//dt1最小值，dt2最大值
        public void InitComponent()
        {
            //NO2Chart.ChartAreas[0].AxisX.IntervalType ;
            Color charLineColor = Color.Red;
            int charLineWidth = 2;
            Color charBackColor = Color.White;
            //Color labelColor = Color.Green;
            //int labelWidth = 2;

            NO2Chart.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            NO2Chart.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            NO2Chart.ChartAreas[0].AxisY.Interval = 4.000;
            NO2Chart.ChartAreas[0].AxisY.Minimum = 0.000;
            NO2Chart.ChartAreas[0].AxisY.Maximum = 20.000;
            NO2Chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            NO2Chart.ChartAreas[0].BackColor = charBackColor;
            NO2Chart.Series[0].Color = charLineColor;
            NO2Chart.Series[0].BorderWidth = charLineWidth;
            //NO2Chart.Series[0].IsValueShownAsLabel = true;
            //NO2Chart.Series[0].LabelForeColor = labelColor;
            //NO2Chart.Series[0].LabelBorderWidth = labelWidth;


            //--------------------------------------------------------------
            COChart.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            COChart.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            COChart.ChartAreas[0].AxisY.Interval = 10.000;
            COChart.ChartAreas[0].AxisY.Minimum = 0.000;
            COChart.ChartAreas[0].AxisY.Maximum = 50.000;
            COChart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            COChart.ChartAreas[0].BackColor = charBackColor;
            COChart.Series[0].Color = charLineColor;
            COChart.Series[0].BorderWidth = charLineWidth;
            //COChart.Series[0].IsValueShownAsLabel = true;
            //COChart.Series[0].LabelForeColor = labelColor;
            //COChart.Series[0].LabelBorderWidth = labelWidth;
            //--------------------------------------------------------------
            NOChart.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            NOChart.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            NOChart.ChartAreas[0].AxisY.Interval = 4.000;
            NOChart.ChartAreas[0].AxisY.Minimum = 0.000;
            NOChart.ChartAreas[0].AxisY.Maximum = 20.000;
            NOChart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            NOChart.ChartAreas[0].BackColor = charBackColor;
            NOChart.Series[0].Color = charLineColor;
            NOChart.Series[0].BorderWidth = charLineWidth;
            //NOChart.Series[0].IsValueShownAsLabel = true;
            //NOChart.Series[0].LabelForeColor = labelColor;
            //NOChart.Series[0].LabelBorderWidth = labelWidth;
            //--------------------------------------------------------------
            O3Chart.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            O3Chart.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            O3Chart.ChartAreas[0].AxisY.Interval = 4.000;
            O3Chart.ChartAreas[0].AxisY.Minimum = 0.000;
            O3Chart.ChartAreas[0].AxisY.Maximum = 20.000;
            O3Chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            O3Chart.ChartAreas[0].BackColor = charBackColor;
            O3Chart.Series[0].Color = charLineColor;
            O3Chart.Series[0].BorderWidth = charLineWidth;
            //O3Chart.Series[0].IsValueShownAsLabel = true;
            //O3Chart.Series[0].LabelForeColor = labelColor;
            //O3Chart.Series[0].LabelBorderWidth = labelWidth;
            //--------------------------------------------------------------
            TemChart.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            TemChart.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            TemChart.ChartAreas[0].AxisY.Interval = 10;
            TemChart.ChartAreas[0].AxisY.Minimum = -10;
            TemChart.ChartAreas[0].AxisY.Maximum = 50;
            TemChart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            TemChart.ChartAreas[0].BackColor = charBackColor;
            TemChart.Series[0].Color = charLineColor;
            TemChart.Series[0].BorderWidth = charLineWidth;
            //TemChart.Series[0].IsValueShownAsLabel = true;
            //TemChart.Series[0].LabelForeColor = labelColor;
            //TemChart.Series[0].LabelBorderWidth = labelWidth;
            //--------------------------------------------------------------
            RHChart.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            RHChart.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            RHChart.ChartAreas[0].AxisY.Interval = 0.20;
            RHChart.ChartAreas[0].AxisY.Minimum = 0.00;
            RHChart.ChartAreas[0].AxisY.Maximum = 1.00;
            RHChart.ChartAreas[0].AxisY.LabelStyle.Format = "0%";//格式化，为了显示百分号
            RHChart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            RHChart.ChartAreas[0].BackColor = charBackColor;
            RHChart.Series[0].Color = charLineColor;
            RHChart.Series[0].BorderWidth = charLineWidth;
            //RHChart.Series[0].IsValueShownAsLabel = true;
            //RHChart.Series[0].LabelForeColor = labelColor;
            //RHChart.Series[0].LabelBorderWidth = labelWidth;
            //--------------------------------------------------------------
            AtoChart.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            AtoChart.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Tahoma", 7);
            AtoChart.ChartAreas[0].AxisY.Interval = 10;
            AtoChart.ChartAreas[0].AxisY.Minimum = 80;
            AtoChart.ChartAreas[0].AxisY.Maximum = 130;
            AtoChart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            AtoChart.ChartAreas[0].BackColor = charBackColor;
            AtoChart.Series[0].Color = charLineColor;
            AtoChart.Series[0].BorderWidth = charLineWidth;
            //AtoChart.Series[0].IsValueShownAsLabel = true;
            //AtoChart.Series[0].LabelForeColor = labelColor;
            //AtoChart.Series[0].LabelBorderWidth = labelWidth;
            //--------------------------------------------------------------
        }
        public void SetXStepLength(Chart chart1)
        {
            //chart1.ChartAreas[0].AxisX.Interval = DateDiff(Convert.ToDateTime(dt1), Convert.ToDateTime(dt2))/5;
            //chart1.ChartAreas[0].AxisX.Minimum = Convert.ToDateTime(dt1).ToOADate();
            //chart1.ChartAreas[0].AxisX.Maximum = Convert.ToDateTime(dt2).ToOADate();
            chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
            chart1.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = false;
        }     
        public HistoryWindow()
        {
          
            InitializeComponent();
            InitComponent();

            DataBase dataBase = new DataBase();
            bianDianZhan.DataSource = dataBase.getAllBianDianZhan();
            bianDianZhan.DisplayMember = "Bdzmc";
            bianDianZhan.Refresh();
        }
        private ArrayList x = new ArrayList();
        private ArrayList dt = new ArrayList();

        public ArrayList Dt
        {
            get { return dt; }
            set { dt = value; }
        }
        private ArrayList yNO2;

        public ArrayList YNO2
        {
            get { return yNO2; }
            set { yNO2 = value; }
        }
        private ArrayList yNO;

        public ArrayList YNO
        {
            get { return yNO; }
            set { yNO = value; }
        }
        private ArrayList yCO;

        public ArrayList YCO
        {
            get { return yCO; }
            set { yCO = value; }
        }
        private ArrayList yO3;

        public ArrayList YO3
        {
            get { return yO3; }
            set { yO3 = value; }
        }
        private ArrayList yTem;

        public ArrayList YTem
        {
            get { return yTem; }
            set { yTem = value; }
        }
        private ArrayList yRH;

        public ArrayList YRH
        {
            get { return yRH; }
            set { yRH = value; }
        }
        private ArrayList yAto;

        public ArrayList YAto
        {
            get { return yAto; }
            set { yAto = value; }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(dateTimePicker1.Value.ToString("yyyy-MM-dd hh:mm:ss"));
            try
            {
                DataBase dataBase = new DataBase();
                string bdz = bianDianZhan.Text.Trim();
                string kgs = kaiGuanShi.Text.Trim();
                string kgg = kaiGuanGui.Text.Trim();
                DateTime rdt1 = dateTimePicker1.Value;
                DateTime rdt2 = dateTimePicker2.Value;
                yNO2 = dataBase.getHistory3(rdt1,rdt2, "NO2", bdz, kgs, kgg,-1)[0];
                yNO = dataBase.getHistory3(rdt1, rdt2, "NO", bdz, kgs, kgg, -1)[0];
                yCO = dataBase.getHistory3(rdt1, rdt2, "CO", bdz, kgs, kgg, -1)[0];
                yO3 = dataBase.getHistory3(rdt1, rdt2, "O3", bdz, kgs, kgg, -1)[0];
                yTem = dataBase.getHistory3(rdt1, rdt2, "Tem", bdz, kgs, kgg, -1)[0];
                yRH = dataBase.getHistory3(rdt1, rdt2, "RH", bdz, kgs, kgg, -1)[0];
                yAto = dataBase.getHistory3(rdt1, rdt2, "Ato", bdz, kgs, kgg, -1)[0];
                Dt = dataBase.getHistory3(rdt1, rdt2, "NO2", bdz, kgs, kgg, -1)[1];
       
                updateData(NO2Chart);
                updateData(NOChart);
                updateData(COChart);
                updateData(O3Chart);
                updateData(TemChart);
                updateData(RHChart);
                updateData(AtoChart);
                SetXStepLength(NO2Chart);
                SetXStepLength(NOChart);
                SetXStepLength(COChart);
                SetXStepLength(O3Chart);
                SetXStepLength(TemChart);
                SetXStepLength(RHChart);
                SetXStepLength(AtoChart);
            }
            catch (Exception ex)
            {
                StreamWriter sw = new StreamWriter(System.Environment.CurrentDirectory+"\\log.txt", true, Encoding.Unicode, 4096);
                sw.WriteLine(ex.Message + DateTime.Now.ToLongTimeString()+"click_History");
                sw.Close();
            }
        }
        private int DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            int  dateDiff = 0;
            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new
            TimeSpan(DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            dateDiff = ts.Days*24*3600+ts.Hours*3600 + ts.Minutes*60 + ts.Seconds;
            return dateDiff;
        }
        public void GetMaxAndMin(out DateTime Max, out DateTime Min, ArrayList al)
        {
            Max = Min = DateTime.Parse(al[0].ToString());
            foreach (DateTime el in al)
            {
                if (DateTime.Compare(Max,el)<0)
                {
                    Max = el;
                }
                if (DateTime.Compare(Min,el)>0)
                {
                    Min = el;
                }
            }
        }
        public void updateData(Chart chart)
        {
          
            switch (chart.Name)
            {
                case "NO2Chart":
                    chart.Series[0].Points.DataBindXY(Dt, yNO2);
                    //chart.Series[0].Points.DataBindXY(dt2222);
                    break;
                case "NOChart":
                    chart.Series[0].Points.DataBindXY(Dt, yNO);
                    break;
                case "COChart":

                    chart.Series[0].Points.DataBindXY(Dt, yCO);
                    break;
                case "O3Chart":
                    chart.Series[0].Points.DataBindXY(Dt, yO3);
                    break;
                case "TemChart":
                    chart.Series[0].Points.DataBindXY(Dt, yTem);
                    break;
                case "RHChart":
                    chart.Series[0].Points.DataBindXY(Dt, yRH);
                    break;
                case "AtoChart":
                    chart.Series[0].Points.DataBindXY(Dt, yAto);
                    break;
            }

        }

        private void HistoryWindow_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“sCCMDataSet1.KGGBH”中。您可以根据需要移动或删除它。

            dateTimePicker1.Value = DateTime.Now.Date;
            dateTimePicker2.Value = DateTime.Now.Date;
        }

        private void NO2Chart_Click(object sender, EventArgs e)
        {
            if (dt.Count == 0||dt==null)
            {
                return ;
            }
            PublicPlotUseArrayList publicPlot = new PublicPlotUseArrayList("NO2");
            publicPlot.SetX(dt);
            publicPlot.SetY(YNO2);
          //  MessageBox.Show(dt.Count.ToString() + YNO2.Count.ToString());
            publicPlot.SetTile("NO2");
            publicPlot.SetForm(this);
           publicPlot.Show();
        }

        private void bianDianZhan_SelectedIndexChanged(object sender, EventArgs e)
        {

            DataRowView drv = (DataRowView)bianDianZhan.SelectedItem;
            string bdz = drv.Row["Bdzmc"].ToString();
            if ("".Equals(bdz) || null == bdz)
            {
                return;
            }
            DataBase dataBase = new DataBase();
            kaiGuanShi.DataSource = dataBase.getAllKaiGuanShiByBDZ(bdz);
            kaiGuanShi.DisplayMember = "Kgsmc";
            kaiGuanShi.Refresh();
        }

        private void bianDianZhan_SelectionChangeCommitted(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)bianDianZhan.SelectedItem;
            string bdz = drv.Row["Bdzmc"].ToString();
            if ("".Equals(bdz) || null == bdz)
            {
                return;
            }
            DataBase dataBase = new DataBase();
            kaiGuanShi.DataSource = dataBase.getAllKaiGuanShiByBDZ(bdz);
            kaiGuanShi.DisplayMember = "Kgsmc";
            kaiGuanShi.Refresh();
        }

        private void kaiGuanShi_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)bianDianZhan.SelectedItem;
            string bdz = drv.Row["Bdzmc"].ToString();
            DataRowView drv2 = (DataRowView)kaiGuanShi.SelectedItem;
            string kgs = drv2.Row["Kgsmc"].ToString();
            if ("".Equals(kgs) || null == kgs)
            {
                return;
            }
            if ("".Equals(bdz) || null == bdz)
            {
                return;
            }
            DataBase dataBase = new DataBase();
            kaiGuanGui.DataSource = dataBase.getAllKaiGuanGuiByBdzAndKgs(bdz, kgs);
            kaiGuanGui.DisplayMember = "Kggbh";
            kaiGuanGui.Refresh();
        }

        private void kaiGuanShi_SelectionChangeCommitted(object sender, EventArgs e)
        {
             DataRowView drv = (DataRowView)bianDianZhan.SelectedItem;
            string bdz = drv.Row["Bdzmc"].ToString();
            DataRowView drv2 = (DataRowView)kaiGuanShi.SelectedItem;
            string kgs = drv2.Row["Kgsmc"].ToString();
            if ("".Equals(kgs) || null == kgs)
            {
                return;
            }
            if ("".Equals(bdz) || null == bdz)
            {
                return;
            }
            DataBase dataBase = new DataBase();
            kaiGuanGui.DataSource = dataBase.getAllKaiGuanGuiByBdzAndKgs(bdz, kgs);
            kaiGuanGui.DisplayMember = "Kggbh";
            kaiGuanGui.Refresh();
        }

        private void NOChart_Click(object sender, EventArgs e)
        {
            if (dt.Count == 0 || dt == null)
            {
                return;
            }
            PublicPlotUseArrayList publicPlot = new PublicPlotUseArrayList("NO");
            publicPlot.SetX(dt);
            publicPlot.SetY(YNO);
          //  MessageBox.Show(dt.Count.ToString() + YNO.Count.ToString());
            publicPlot.SetTile("NO");
            publicPlot.SetForm(this);
            publicPlot.Show();
        }

        private void COChart_Click(object sender, EventArgs e)
        {
            if (dt.Count == 0 || dt == null)
            {
                return;
            }
            PublicPlotUseArrayList publicPlot = new PublicPlotUseArrayList("CO");
            publicPlot.SetX(dt);
            publicPlot.SetY(YCO);
           // MessageBox.Show(dt.Count.ToString() + YCO.Count.ToString());
            publicPlot.SetTile("CO");
            publicPlot.SetForm(this);
            publicPlot.Show();
        }

        private void O3Chart_Click(object sender, EventArgs e)
        {
            if (dt.Count == 0 || dt == null)
            {
                return;
            }
            PublicPlotUseArrayList publicPlot = new PublicPlotUseArrayList("O3");
            publicPlot.SetX(dt);
            publicPlot.SetY(YO3);
           // MessageBox.Show(dt.Count.ToString() + YO3.Count.ToString());
            publicPlot.SetTile("O3");
            publicPlot.SetForm(this);
            publicPlot.Show();
        }

        private void TemChart_Click(object sender, EventArgs e)
        {
            if (dt.Count == 0 || dt == null)
            {
                return;
            }
            PublicPlotUseArrayList publicPlot = new PublicPlotUseArrayList("Tem");
            publicPlot.SetX(dt);
            publicPlot.SetY(YTem);
           // MessageBox.Show(dt.Count.ToString() + YTem.Count.ToString());
            publicPlot.SetTile("Tem");
            publicPlot.SetForm(this);
            publicPlot.Show();
        }

        private void RHChart_Click(object sender, EventArgs e)
        {
            if (dt.Count == 0 || dt == null)
            {
                return;
            }
            PublicPlotUseArrayList publicPlot = new PublicPlotUseArrayList("RH");
            publicPlot.SetX(dt);
            publicPlot.SetY(YRH);
            //MessageBox.Show(dt.Count.ToString() + YRH.Count.ToString());
            publicPlot.SetTile("RH");
            publicPlot.SetForm(this);
            publicPlot.Show();
        }

        private void AtoChart_Click(object sender, EventArgs e)
        {
            if (dt.Count == 0 || dt == null)
            {
                return;
            }
            PublicPlotUseArrayList publicPlot = new PublicPlotUseArrayList("Ato");
            publicPlot.SetX(dt);
            publicPlot.SetY(YAto);
            //MessageBox.Show(dt.Count.ToString() + YAto.Count.ToString());
            publicPlot.SetTile("Ato");
            publicPlot.SetForm(this);
            publicPlot.Show();
        }

        private void HistoryWindow_SizeChanged(object sender, EventArgs e)
        {
            int pwidth = Convert.ToInt32(skinPanel2.Width * 0.98);
            int height = Convert.ToInt32(skinPanel2.Height * 0.90);
            NO2GroupBox.Width = pwidth / 2;
            COGroupBox.Left = NO2GroupBox.Width + Convert.ToInt32(0.02 * skinPanel2.Width);
            COGroupBox.Width = pwidth / 2;
            O3GroupBox.Width = pwidth / 2;
            NOGroupBox.Left = pwidth / 2 + Convert.ToInt32(0.02 * skinPanel2.Width);
            NOGroupBox.Width = pwidth / 2;

            int subx = Convert.ToInt32((COGroupBox.Right - NO2GroupBox.Left) * 0.98);
            AtoGroupBox.Width = subx / 3;
            RHGroupBox.Left = COGroupBox.Right - subx / 3;
            RHGroupBox.Width = subx / 3;
            TemGroupBox.Left = AtoGroupBox.Right + (RHGroupBox.Left - AtoGroupBox.Right - subx / 3) / 2;
            TemGroupBox.Width = subx / 3;


            NO2GroupBox.Height = height / 3;
            COGroupBox.Height = height / 3;

            O3GroupBox.Top = NOGroupBox.Top = NO2GroupBox.Bottom + Convert.ToInt32(skinPanel2.Height * 0.02);
            O3GroupBox.Height = NOGroupBox.Height = height / 3;


            AtoGroupBox.Top = TemGroupBox.Top = RHGroupBox.Top = O3GroupBox.Bottom + Convert.ToInt32(skinPanel2.Height * 0.02);
            AtoGroupBox.Height = TemGroupBox.Height = RHGroupBox.Height = height / 3;

            ////////////
            int tmp = groupBox1.Height;
            groupBox1.Height = tmp;
        }
    }
}
